<?php 
/*
 * Author : Avik Kundu
 * Created On : 01/07/2011
 */

//===============File Include================
include_once("../ImageResizeFactory.php");
//===========================================


//================ Getting the details of image files in the upload folder==================
$allowedExtensions = array("jpg", "JPG", "JPEG", "png", "PNG");
$file = scandir("../upload");
$imageFiles = array();
foreach($file as $key => $value) 
{
	$exploded = explode(".",$value);
	if(in_array($exploded[1],$allowedExtensions)) 
    {//if this is an image file then taking these in an array
    	array_push($imageFiles,$value);
    }
}
//print "<pre>";
//print_r($imageFiles);
//print "</pre>";
//================ Getting the details of image files in the upload folder==================


//################################################################
	foreach($imageFiles as $key=>$values)
	{    
		
		$size = getimagesize("../upload/".$values); 
		$width = $size[0];
		$height = $size[1];
		if($width > 600)
		{
			$width = ($width*0.50);
		}  
		if($height > 500)
		{
			$height = ($height*0.50);
		} 
		if($width==0) $width=500;
		if($height==0) $height=400;
		if($width > 600) $width=500;
		if($height > 500) $height=400;
		
	    $allowedExtensions = array("jpg", "JPG", "JPEG", "png", "PNG");
		$uploadedFileName = $values;
		$extension = pathinfo($values);
		$extension = $extension["extension"];
		foreach($allowedExtensions as $key=>$ext) {
			if(strcasecmp($ext, $extension) == 0)
			{
				$boolValidExt = true;
				break;
			}
		}
		if($boolValidExt) {
			if(empty($error)) {
				if(is_uploaded_file($values)) {
					copy($values, "../upload/" . $uploadedFileName);
				}
			}
		}
		else
		{
			$error = "Files with .$extension extensions are not allowed";
		}
		if(empty($error))
		{
			$srcFile1 = "../upload/" . $uploadedFileName;
			$destFile1 = "../upload/resized_" . $uploadedFileName;
	
			// Instantiate the correct object depending on type of image i.e jpg or png
			$objResize = ImageResizeFactory::getInstanceOf($srcFile1, $destFile1, $width, $height);
			// Call the method to resize the image
			$objResize->getResizedImage();
			//unlink($srcFile1);
		}
	} 
	//##################################################################





